/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.vineatoum.view.component;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.TransferHandler;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.datatransfer.Transferable;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.vineatoum.business.state.abstraction.VinAbstract;
import org.vineatoum.business.tracking.TrackInfo;
import org.vineatoum.view.utils.TransferableVinAbstract;

import javax.swing.ListSelectionModel;

public class TrackingPanel extends JPanel {
	private static final long serialVersionUID = -4870935964931177937L;
	
	private JTable table;
	private JTable table_1;

	/**
	 * Create the panel.
	 */
	public TrackingPanel(VinAbstract vinAbstract) {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{450, 0};
		gridBagLayout.rowHeights = new int[]{14, 0, 14, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		TrackInfo trackInfo = vinAbstract.getTrackingInfo();
		List<TrackInfo> origines = trackInfo.getFromElements();
		
Object [][] data = new Object[origines.size()][2];
		
		for (int i = 0; i < origines.size(); i++) {
			TrackInfo track = origines.get(i);
			VinAbstract vin = track.getTrackedElement();
			data[i][0] = vin;
			data[i][1] = vin.getName();
		}
		
		table = new JTable();
		
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setDragEnabled(true);
		table.setModel(new DefaultTableModel(
			data,
			new String[] {
				"Nom", "Etat"
			}
		));
		table.setTransferHandler(new TransferHandler()
		{

			@Override
			protected Transferable createTransferable(JComponent c) {
				// TODO Auto-generated method stub
				JTable t = (JTable) c;
				Object o =  t.getModel().getValueAt(t.getSelectedRow(), t.getSelectedColumn());
				System.out.println(o.getClass());
				if(o instanceof VinAbstract)
				{
					return new TransferableVinAbstract((VinAbstract) o);
				}
				
				return null;
				
			}

			@Override
			public int getSourceActions(JComponent c) {
				// TODO Auto-generated method stub
				return TransferHandler.MOVE;
			}
			
		});
		
		GridBagConstraints gbc_lblSuccesseurs_2 = new GridBagConstraints();
		gbc_lblSuccesseurs_2.insets = new Insets(0, 0, 5, 0);
		gbc_lblSuccesseurs_2.gridx = 0;
		gbc_lblSuccesseurs_2.gridy = 2;
				
				
		List<TrackInfo> children = trackInfo.getChildElements();
		
		data = new Object[children.size()][2];
		
		for (int i = 0; i < children.size(); i++) {
			TrackInfo track = children.get(i);
			VinAbstract vin = track.getTrackedElement();
			data[i][0] = vin;
			data[i][1] = vin.getClass();
		}
		
		table_1 = new JTable();
		table_1.setDragEnabled(true);
		table_1.setModel(new DefaultTableModel(
			data,
			new String[] {
				"Nom", "Etat"
			}
		));
		table_1.setTransferHandler(new TransferHandler()
		{

			@Override
			protected Transferable createTransferable(JComponent c) {
				// TODO Auto-generated method stub
				JTable t = (JTable) c;
				Object o =  t.getModel().getValueAt(t.getSelectedRow(), t.getSelectedColumn());
				
				return new TransferableVinAbstract((VinAbstract) o);
			}

			@Override
			public int getSourceActions(JComponent c) {
				// TODO Auto-generated method stub
				return TransferHandler.COPY;
			}
			
		}
		);
		
		JLabel lblAntcdents = new JLabel("Ant\u00E9c\u00E9dents");
		GridBagConstraints gbc_lblAntcdents = new GridBagConstraints();
		gbc_lblAntcdents.anchor = GridBagConstraints.WEST;
		gbc_lblAntcdents.insets = new Insets(0, 0, 5, 0);
		gbc_lblAntcdents.gridx = 0;
		gbc_lblAntcdents.gridy = 0;
		add(lblAntcdents, gbc_lblAntcdents);
		
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(table, BorderLayout.CENTER);
		panel.add(table.getTableHeader(), BorderLayout.NORTH);
		
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.anchor = GridBagConstraints.NORTH;
		gbc_scrollPane.fill = GridBagConstraints.HORIZONTAL;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		add(panel, gbc_scrollPane);
				
				
		JLabel lblSuccesseurs = new JLabel("Successeurs");
		GridBagConstraints gbc_lblSuccesseurs_1 = new GridBagConstraints();
		gbc_lblSuccesseurs_1.anchor = GridBagConstraints.WEST;
		gbc_lblSuccesseurs_1.insets = new Insets(0, 0, 5, 0);
		gbc_lblSuccesseurs_1.gridx = 0;
		gbc_lblSuccesseurs_1.gridy = 2;
		add(lblSuccesseurs, gbc_lblSuccesseurs_1);
		
		JPanel panel_1 = new JPanel(new BorderLayout());
		panel_1.add(table_1, BorderLayout.CENTER);
		panel_1.add(table_1.getTableHeader(), BorderLayout.NORTH);
		
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.anchor = GridBagConstraints.NORTH;
		gbc_scrollPane_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_scrollPane_1.gridx = 0;
		gbc_scrollPane_1.gridy = 3;
		add(panel_1, gbc_scrollPane_1);
	}

}
